//oauth接口
import {_axios, makeStringifyUrl, urlParamStringify} from '@/plugins/axios'
import {securityStore} from "./observable";

export const securityApi = {
  securityUrl: {
    TOKEN: '/oauth/token',
    CHECK: '/oauth/check_token',
    LOGOUT:'/oauth/logout',
    ACCREDIT: '/security/accredit',
    AUTHUSER: '/security/currUser',

  },
  clientSecret: {
    client_id: process.env.VUE_APP_SECURITY_CLIENT_ID,
    client_secret: process.env.VUE_APP_SECURITY_CLIENT_SECRET
  },
  //检查token
  //check: async (params) => await _axios.post(this.oauthUrl.CHECK + "?" + _qs.stringify(params)),
  async check(params) {
    return await _axios.post(this.securityUrl.CHECK + "?" + urlParamStringify(params))
  },
  //获取token
  async token(params) {
    //return await _axios.post( "/security/loginVerify?" + urlParamStringify({...this.clientSecret, ...params, grant_type: "password"}))
    return await _axios.post(this.securityUrl.TOKEN + "?" + urlParamStringify({...this.clientSecret, ...params, grant_type: "password"}))
  },
  //刷新token
  async refresh(params) {
    return await _axios.post(this.securityUrl.TOKEN + "?" + urlParamStringify({...this.clientSecret, ...params, grant_type: "refresh_token"}))
  },
  //获取权限
  async accredit() {
    return await _axios.get(this.securityUrl.ACCREDIT )
  },
  //校验码
  verifyImg() {
    return makeStringifyUrl("/security/verifyImage",{tag:(new Date().getTime())})
  },
  //获取用户
  async authUser() {
    return await _axios.get(this.securityUrl.AUTHUSER) ;
  },
  async logout(){
    return await _axios.get(this.securityUrl.LOGOUT) ;
  },
  //构建带权限的url
   makeAuthStringifyUrl(shortUrl,param){
    return makeStringifyUrl(shortUrl,{...param,[security_const.access_token]:securityStore.observe.oauthToken})
  }
}
